<?php
class SessionAction extends BaseAction{
	public function login(){
		if (service('Passport')->isLogin()) {
			$this->assign('ttest','rkkkkkkkun');
		}
		import("ORG.Util.Sina");
		$sina = new Sina();
		//
		$pageModel = array();
		$pageModel['sina_url'] = $sina->getUrl();
		//
		$this->assign($pageModel);
		$this->display();
	}

	public function session(){
		if ( !in_array($_SESSION['open_platform_type'], array('sina')) ) {
			$this->error('授权失败');
		}
		redirect(U("Index/index"));
	}

	public function logout(){
		$service('Passport')-> doLogout();
	}

	public function callback(){
		import("ORG.Util.Sina");
		$sina = new Sina();
		$sina->checkUser();
		$userinfo = $sina->userInfo();
		if ( empty($userinfo['uid']) || empty($userinfo['name']) ) {
			//$this->assign('jumpUrl', SITE_URL);
			$this->error('获取用户信息失败');
		}
		//检查是否已经加入本站
		$User = new UserModel('User');
		$cur_user = $User-> where('uid= '.$userinfo['uid'])->find();
		
		
		if(!$cur_user){
			//初始化用户信息
			$userinfo['add_time']=time();
			$userinfo['update_time']=time();
			$User->add($userinfo);
		}else{
			$userinfo['update_time']=time();
			$User->where('id='.$cur_user['id'])->save($userinfo);
		}
		//记录登录的log
		$LoginRecord = new LoginRecordModel('LoginRecord');
		$log['uid'] = $userinfo['uid'];
		$log['login_time'] =time();
		$log['ip'] = get_client_ip();
		$log['useragent'] = get_user_agent();
		$LoginRecord-> add($log);
		//
//		$pageModel = array();
//		$pageModel['userinfo'] = $userinfo;
//		$pageModel['count']= $User->findAll();
//		//
//		$this->assign($pageModel);
//		
//		$this-> display();
		redirect('Tweet/tweets');
	}


}
?>